#include "Max6675.h"

#include "Max6675Config.h"

void Max6675_init()
{
    SPI_init(MAX6675_SPI);
    GPIO_setMode(MAX6675_SPI_MOSI_Port, MAX6675_SPI_MOSI_Pin, GPIO_Mode_OutPP);
}

double Max6675_getT()
{
    uint16_t s = 0;
    double result = 0;
    SPI_setSize(MAX6675_SPI, SPMD_bit16);
    SPI_begin(MAX6675_SPI);
    SPI_sendWord(MAX6675_SPI, 0);
    while(!SPI_sendFinish(MAX6675_SPI));
    s = SPI_receiveWord(MAX6675_SPI);
    s = s >> 2;
    SPI_clearFlag(MAX6675_SPI);
    SPI_end(MAX6675_SPI);
    GPIO_setBit(MAX6675_SPI_MOSI_Port, MAX6675_SPI_MOSI_Pin, 1);
    result = ((double) s) * 0.25 - MAX6675_INI_TEMP;
    return result;
}

